<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/26
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserModel extends BaseModel{

    protected $table = 'user';

    public function ManageUserList()
    {
        return DB::table($this->table)
            ->orderBy('user_id','desc')
            ->get();
    }


    public function GetUserInfo($id)
    {
        $user_info = DB::table($this->table)
            ->select('user_id','user_name','name','is_idcard','idcard','telephone','add_time','password','is_locked')
            ->where('user_id',$id)
            ->first();
        return $user_info;
    }
     
    /**
     * 根据用户id更新用户信息
     * @param int $user_id
     * @param array $data
     * @param mixed
     */
    public function updateByUserId($user_id, $data) 
    {
        return DB::table($this->table)->where(['user_id' => $user_id])->update($data);
   	}


    /**
     * 获取所有用户ID
     * @param mixed
     */
    public function GetUserIdAll()
    {
        return DB::table($this->table)
            ->select("user_id")
            ->get();
    }

    /**
     * 注册手机号js验证
     * @param $mobile
     * @return mixed
     */
    public function JsCheckMobile($mobile)
    {
        return DB::table($this->table)
            ->select('telephone','password','user_id','salt')
            ->where('telephone',$mobile)
            ->first();
    }

    /**
     * 注册验证码验证
     * @param $code
     * @return mixed
     */
    public function JsCheckCode($code)
    {
        return DB::table($this->table)
            ->select('invite_code')
            ->where('invite_code',$code)
            ->first();
    }

    /**
     * 获取邀请人id
     * @param $code
     * @return mixed
     */
    public function CheckInviteId($code)
    {
        return DB::table($this->table)
            ->select('user_id')
            ->where('invite_code',$code)
            ->first();
    }


    public function GetUserIphone($user_id){
        return DB::table($this->table)
            ->select('telephone')
            ->where('user_id',$user_id)
            ->first();
    }

    public function UserInfo($user_id){
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->first();
    }


    public function UserInfoInvite($user_id){
        return DB::table($this->table)
            ->select('invite_code')
            ->where('user_id',$user_id)
            ->first();
    }


    /**
     * 根据你手机号查询是否存在
     * @param mixed
     */
    public function UserFirphone($phone)
    {
        return DB::table($this->table)
            ->select('user_id','user_name','name','is_locked','telephone','salt','password','is_idcard','idcard')
            ->where('telephone',$phone)
            ->first();
    }

    /**
     * 根据uid查询
     * @param mixed
     */
    public function UserInfofirst($user_id)
    {
        return DB::table($this->table)
            ->select('user_id','user_name','name','is_locked','telephone','is_idcard','salt','password','idcard','invite_code')
            ->where('user_id',$user_id)
            ->first();
    }



    /**
     * 检查是否实名
     */
    public function Iscard($paramObj){
        return DB::table($this->table)
            ->select('is_idcard')
            ->where('user_id',$paramObj->user_id)
            ->first();
    }

    /**
     * 邀请人数
     */
    public function UserInviteFriendsCount($user_id,$time){
        return DB::table($this->table)
            ->select('user_id')
            ->where('parent_id',$user_id)
            ->whereBetween('add_time', $time)
            ->groupBy('parent_id')
            ->count();
    }

    /**
     * @return mixed
     * 用户邀请列表
     */
    public function InviteUserList($end_time)
    {
        return DB::table($this->table)
            ->select('user_id','telephone','name')
            ->where('add_time','<',$end_time)
            ->orderBy('user_id','desc')
            ->get();
    }

    /**
     * @return mixed
     * 用户邀请用户ID
     */
    public function UserInviteId($user_id,$time)
    {
        return DB::table($this->table)
            ->select('user_id')
            ->where('parent_id',$user_id)
            ->whereBetween('add_time', $time)
            ->get();
    }

    /**
     * @return mixed
     * 用户邀请用户ID1111111111111111
     */
    public function UserInviteId1($user_id,$time)
    {
        return DB::table($this->table)
            ->select('user_id')
            ->whereBetween('add_time', $time)
            ->whereIn('parent_id',$user_id)
            ->get();
    }


    /**
     * @return mixed
     * 用户邀请用户人数
     */
    public function InviteCount($user_id,$time)
    {
        return DB::table($this->table)
            ->select('user_id')
            ->whereBetween('add_time', $time)
            ->where('parent_id',$user_id)
            ->count();
    }

    /**
     * 用户邀请人信息
     * @param $user_id
     * @return mixed
     */
    public function UserInvite($user_id,$page,$page_size,$time)
    {
        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        return  DB::table($this->table)
            ->select('user_id','user_name','telephone','add_time')
            ->where('parent_id',$user_id)
            ->whereBetween('add_time', $time)
            ->skip($page)->take($page_size)
            ->get();
    }

    /**
     * 用户邀请人信息
     * @param $user_id
     * @return mixed
     */
    public function InvestAmount($user_id,$time)
    {
        return  DB::table($this->table)
            ->select('user_id')
            ->where('parent_id',$user_id)
            ->whereBetween('add_time', $time)
            ->get();
    }


    /**
     * 用户邀请人信息
     * @param $user_id
     * @return mixed
     */
    public function ApiUserInvite($user_id,$page,$page_size,$time)
    {
        if($page == 1){
            $page = $page - 1;
        }else{
            $page =$page_size*($page-1);
        }
        return  DB::table($this->table)
            ->select('user_id','user_name','add_time')
            ->where('parent_id',$user_id)
            ->whereBetween('add_time', $time)
            ->skip($page)->take($page_size)
            ->get();
    }


    /**
     * Api根据你手机号查询是否存在
     * @param mixed
     */
    public function ApiUserFirphone($phone)
    {
        return DB::table($this->table)
            ->select('user_id','user_name','name','is_locked','telephone','is_idcard','idcard','invite_code')
            ->where('telephone',$phone)
            ->first();
    }


    /**
     * web根据uid查询
     * @param mixed
     */
    public function WebUserInfofirst($user_id)
    {
        return DB::table($this->table)
            ->select('user_id','user_name','name','is_locked','telephone','is_idcard','idcard')
            ->where('user_id',$user_id)
            ->first();
    }


}